﻿using AutoMapper.Internal.Mappers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ThridGroup.ERP.DTO.MaterielDto;
using ThridGroup.ERP.DTO.ProductionSystemDto.CustomerOrderDto;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace ThridGroup.ERP.ProductionSystem
{
    /// <summary>
    /// 客户订单服务
    /// </summary>
    public  class CustomerOrderServices: ApplicationService, ICustomerOrderServices
    {
        /// <summary>
        /// 客户仓储
        /// </summary>
        private readonly IRepository<CustomerModel,int> _customerModelServices;
        /// <summary>
        /// 客户订单仓储
        /// </summary>
        private readonly IRepository<CustomerOrder,int> _customerOrderServices;

        public CustomerOrderServices(IRepository<CustomerModel, int> customerModelServices, IRepository<CustomerOrder, int> customerOrderServices)
        {
            _customerModelServices = customerModelServices;
            _customerOrderServices = customerOrderServices;
        }

        /// <summary>
        /// 显示客信息
        /// </summary>
        /// <param name="canGetCustomerDto"></param>
        /// <returns></returns>
        public async Task<List<QueryCustomerModelDto>> QueryCustomer(GetCustomerDto canGetCustomerDto) 
        {
            var customerlist = await _customerModelServices.GetListAsync();
            //查询客户编号
            customerlist = customerlist.WhereIf(!string.IsNullOrEmpty(canGetCustomerDto.CustomerCode), a => a.CustomerCode.Contains(canGetCustomerDto.CustomerCode)).ToList();
            //查询客户名称
            customerlist = customerlist.WhereIf(!string.IsNullOrEmpty(canGetCustomerDto.CustomerCode), a => a.CustomerCode.Contains(canGetCustomerDto.CustomerName)).ToList();
           
            //按物料分类查询
            customerlist = customerlist.WhereIf(canGetCustomerDto.IsEnable !=null, a => a.IsEnable == canGetCustomerDto.IsEnable).ToList();
            return ObjectMapper.Map<List<CustomerModel>, List<QueryCustomerModelDto>>(customerlist);
        }

        /// <summary>
        /// 添加客户订单
        /// </summary>
        public async Task<int> CreateCustomerOrder(CustomerOrderlistDto dto)
        {
            var entity = ObjectMapper.Map<CustomerOrderlistDto, CustomerOrder>(dto);
            entity.CreateTime = DateTime.Now;
            entity.UpdateTime = DateTime.Now;
            var result = await _customerOrderServices.InsertAsync(entity);
            return result == null ? 0 : 1;
        }

        /// <summary>
        /// 查询客户订单（分页/条件）
        /// </summary>
        public async Task<List<CustomerOrderlistDto>> QueryCustomerOrder(GetCustomerOrderDto getCustomerOrderDto)
        {
            var orderList = await _customerOrderServices.GetListAsync();
            // 订单编码
            orderList = orderList.WhereIf(!string.IsNullOrEmpty(getCustomerOrderDto.OrderCode), o => o.OrderCode.Contains(getCustomerOrderDto.OrderCode)).ToList();
            // 客户名称
            orderList = orderList.WhereIf(!string.IsNullOrEmpty(getCustomerOrderDto.CustomerName), o => o.CustomerName.Contains(getCustomerOrderDto.CustomerName)).ToList();
            // 订货日期区间
            orderList = orderList
                .WhereIf(getCustomerOrderDto.OrderTimeStart.HasValue, o => o.OrderTime >= getCustomerOrderDto.OrderTimeStart.Value)
                .WhereIf(getCustomerOrderDto.OrderTimeEnd.HasValue, o => o.OrderTime <= getCustomerOrderDto.OrderTimeEnd.Value)
                .ToList();
            // 交货日期区间
            orderList = orderList
                .WhereIf(getCustomerOrderDto.DeliveryStart.HasValue, o => o.Delivery >= getCustomerOrderDto.DeliveryStart.Value)
                .WhereIf(getCustomerOrderDto.DeliveryEnd.HasValue, o => o.Delivery <= getCustomerOrderDto.DeliveryEnd.Value)
                .ToList();
          
            return ObjectMapper.Map<List<CustomerOrder>, List<CustomerOrderlistDto>>(orderList);
        }

        /// <summary>
        /// 修改客户订单
        /// </summary>
        public async Task<int> UpdateCustomerOrder(UpdateCustomerOrderDto dto)
        {
            var entity = ObjectMapper.Map<UpdateCustomerOrderDto, CustomerOrder>(dto);
            entity.UpdateTime = DateTime.Now;
            var result = await _customerOrderServices.UpdateAsync(entity);
            return result == null ? 0 : 1;
        }

        /// <summary>
        /// 批量删除客户订单
        /// </summary>
        public async Task BatchDeleteCustomerOrder(List<int> ids)
        {
            if (ids == null || !ids.Any())
                return;
            await _customerOrderServices.DeleteDirectAsync(o => ids.Contains(o.Id));
        }
    }
}
